package com.atguigu.lease.common.exception;

import com.atguigu.lease.common.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public Result exceptionHandler(Exception e) {
        System.out.println("发生通用异常：" + e.getMessage());
        Result result = new Result();
        result.setData(e.getMessage());
        result.setMessage(e.getMessage());
        result.setCode(500);
        return result;
    }

    @ExceptionHandler(value = RuntimeException.class)
    public Result runtimeExceptionHandler(RuntimeException e) {
        System.out.println("发生运行时异常：" + e.getMessage());
        Result result = new Result();
        result.setData(e.getMessage());
        result.setMessage(e.getMessage());
        result.setCode(500);
        return result;
    }

    @ExceptionHandler(value = ArithmeticException.class)
    public Result arithmeticExceptionHandler(ArithmeticException e) {
        System.out.println("发生数学运算异常：" + e.getMessage());
        Result result = new Result();
        result.setData(e.getMessage());
        result.setMessage(e.getMessage());
        result.setCode(500);
        return result;
    }
}
